package com.nuc.kl.module.kpoint.dao.impl;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import org.springframework.stereotype.Repository;

import com.nuc.kl.base.DaoSupportImpl;
import com.nuc.kl.module.kpoint.dao.KPointDao;
import com.nuc.kl.module.kpoint.entity.Knowledge;
@SuppressWarnings("unchecked")
@Repository
public class KPointDaoImpl extends DaoSupportImpl<Knowledge> implements KPointDao{

	
	public List<Knowledge> searchPageByIndex(int start, int number,
			Map<String ,Object> m) {
		
		String sql = "from Knowledge k where 1=1 ";
		
		Set<Entry<String, Object>> set = m.entrySet();
		Iterator io = set.iterator();
		while (io.hasNext()) {
			Map.Entry<String, Object> me = (Map.Entry<String, Object>) io.next();
			if("name".equals(me.getKey()) && !"".equals(me.getValue())&& (null!=me.getValue())){
				sql += " and k." + me.getKey() + " like '%"+ me.getValue()  +"%'" ;
			}
			if("minPeriod".equals(me.getKey()) && !"".equals(me.getValue())&& (null!=me.getValue())){
				sql += " and k.period >= " + me.getValue() +"";
			}
			if("maxPeriod".equals(me.getKey()) && !"".equals(me.getValue())&& (null!=me.getValue())){
				sql += " and k.period <= " + me.getValue() +"";
			}
			if("description".equals(me.getKey()) && !"".equals(me.getValue())&& (null!=me.getValue())){
				sql += " and k." + me.getKey() + " like '%"+ me.getValue()  +"%'" ;
			}
			if("defficulty".equals(me.getKey()) && !"".equals(me.getValue())&& (null!=me.getValue())){
				sql += " and k." + me.getKey() + " like '%"+ me.getValue()  +"%'" ;
				
			}if("navigation".equals(me.getKey()) && !"".equals(me.getValue())&& (null!=me.getValue())){
				sql += " and k." + me.getKey() + ".id = "+ me.getValue() ;
				
			}if("status".equals(me.getKey()) && !"".equals(me.getValue())&& (null!=me.getValue())){
				sql += " and k." + me.getKey() + " in ("+ me.getValue()  +")" ;
				
			}if("sort".equals(me.getKey()) && !"".equals(me.getValue())&& (null!=me.getValue())){
				sql += " order by " + me.getValue() ;
			}
			if("order".equals(me.getKey()) && !"".equals(me.getValue())&& (null!=me.getValue())){
				sql += " " + me.getValue();
			}		
		}
		
		
		System.out.println(sql);
		return getSession().createQuery(sql)
		.setFirstResult(start)
		.setMaxResults(number)
		.list();
	}

	public List<Knowledge> loadByNav(long id) {
		return getSession().createQuery("FROM Knowledge WHERE nav_id= ?").setParameter(0, id).list();
	}

}
